#!/bin/sh
### BEGIN INIT INFO
# Provides:          wfm
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5 S
# Default-Stop:      0 1 6
# Short-Description: WFM WAN Failover Manager
# Description:       WFM allows active connection monitoring
#                    and automatic failover in dual WAN setups.
### END INIT INFO

CONFIG=/etc/wfm/wfm.conf

if [ -e "$CONFIG" ]
then
    . "$CONFIG"
fi

stop_wfm () {

    if [ -e "$PIDFILE" ]
    then
        echo "Stopping WFM..."
        kill `cat "$PIDFILE"`
        rm "$PIDFILE"
    else
        echo "WFM is not running."
        exit 1
    fi
}

start_wfm () {

    if [ -e "$PIDFILE" ]
    then
        echo "WFM is already running."
        exit 1
    else
        echo "Starting WFM..."
        /usr/bin/wfs.sh
    fi
}

usage () {

    echo "Usage: $0 [ start | stop | restart | routes | status | swap | targets | viewlog ] "
}

case "$1" in
    start)
            start_wfm
            ;;
    stop)
            stop_wfm
            ;;
    restart)
            stop_wfm
            start_wfm
            ;;
    routes)
            /usr/bin/wfm.sh routes $2 $3 $4 $5
            ;;
    status)
            /usr/bin/wfm.sh status
            ;;
    swap)
            /usr/bin/wfm.sh swap
            ;;
    targets)
            /usr/bin/wfm.sh targets $2 $3 $4 $5 $6 $7 $8 $9
            ;;
    viewlog)
            /usr/bin/wfm.sh viewlog $2
            ;;
    *)
            usage
            ;;
esac 
